package com.ailanyin.modules.search.controller;

import cn.hutool.core.thread.ThreadUtil;
import com.ailanyin.common.base.domain.BaseQuery;
import com.ailanyin.common.base.result.AjaxResult;
import com.ailanyin.common.database.result.CommonPage;
import com.ailanyin.modules.search.domain.IkDict;
import com.ailanyin.modules.search.service.IkDictService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * IK拓展分词Controller
 *
 * @author ailanyin
 * @date 2022-10-10
 */
@RestController
@RequestMapping("/search/ikdict")
@Api(tags = "IK拓展分词管理")
public class IkDictController {
    @Autowired
    private IkDictService ikDictService;

    /**
     * 查询IK拓展分词列表
     */
    @ApiOperation(value = "查询列表('system:ikdict:list')")
    @PreAuthorize("hasAuthority('system:ikdict:list')")
    @GetMapping("/list")
    public CommonPage list(IkDict ikDict,
                           @RequestParam(value = "pageSize" , defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<IkDict> list = ikDictService.selectIkDictList(ikDict);
        return CommonPage.restPage(list);
    }


    /**
     * 获取IK拓展分词详细信息
     */
    @ApiOperation(value = "详细信息('system:ikdict:query')")
    @PreAuthorize("hasAuthority('system:ikdict:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ikDictService.selectIkDictById(id));
    }

    /**
     * 新增IK拓展分词
     */
    @ApiOperation(value = "新增('system:ikdict:add')")
    @PreAuthorize("hasAuthority('system:ikdict:add')")
    @PostMapping
    public AjaxResult add(@RequestBody IkDict ikDict) {
        int result = ikDictService.insertIkDict(ikDict);
        if (result > 0) {
            ThreadUtil.execAsync(() -> ikDictService.addDictToFile(ikDict));
        }
        return AjaxResult.toAjax(result);
    }

    /**
     * 修改IK拓展分词
     */
    @ApiOperation(value = "修改('system:ikdict:edit')")
    @PreAuthorize("hasAuthority('system:ikdict:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody IkDict ikDict) {
        int result = ikDictService.updateIkDict(ikDict);
        if (result > 0) {
            ThreadUtil.execAsync(() -> ikDictService.addDictSetToFile());
        }
        return AjaxResult.toAjax(result);
    }

    /**
     * 删除IK拓展分词
     */
    @ApiOperation(value = "删除('system:ikdict:remove')")
    @PreAuthorize("hasAuthority('system:ikdict:remove')")
    @DeleteMapping
    public AjaxResult remove(@RequestBody BaseQuery<Long> ids) {
        int result = ikDictService.deleteIkDictByIds(ids.getIds());
        if (result > 0) {
            ThreadUtil.execAsync(() -> ikDictService.addDictSetToFile());
        }
        return AjaxResult.toAjax(result);
    }

}
